home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 26
/
Cream of the Crop 26.iso
/
program
/
ddj0897.zip
/
RCSC.ZIP
/
LIB51
/
SPRINTF.VA
< prev
next >
Wrap
Text File
|
1997-01-12
|
2KB
|
54 lines
/*
** sprintf(buffer, ctlstring, arg, arg, ...) - Formatted print.
** Operates as described by Kernighan & Ritchie.
** b, c, d, o, s, u, and x specifications are supported.
** Note: b (binary) is a non-standard extension.
*/
sprintf(argc) int argc; {
va_list nxtarg;
int arg, left, pad, cc, length, maxchr, width;
char *ctl, *sptr, str[17];
char *ptr;
cc = 0;
va_start(nxtarg, argc);
ptr = va_arg(nxtarg, int);
ctl = va_arg(nxtarg, int);
while(*ctl) {
if(*ctl!='%') {*ptr++ = *ctl++; ++cc; continue;}
else ++ctl;
if(*ctl=='%') {*ptr++ = *ctl++; ++cc; continue;}
if(*ctl=='-') {left = 1; ++ctl;} else left = 0;
if(*ctl=='0') pad = '0'; else pad = ' ';
if(isdigit(*ctl)) {
width = atoi(ctl++);
while(isdigit(*ctl)) ++ctl;
}
else width = 0;
if(*ctl=='.') {
maxchr = atoi(++ctl);
while(isdigit(*ctl)) ++ctl;
}
else maxchr = 0;
arg = va_arg(nxtarg, int);
sptr = str;
switch(*ctl++) {
case 'c': str[0] = arg; str[1] = NULL; break;
case 's': sptr = arg; break;
case 'd': itoa(arg,str); break;
case 'b': itoab(arg,str,2); break;
case 'o': itoab(arg,str,8); break;
case 'u': itoab(arg,str,10); break;
case 'x': itoab(arg,str,16); break;
default: *ptr = 0; return (cc);
}
length = strlen(sptr);
if(maxchr && maxchr<length) length = maxchr;
if(width>length) width = width - length; else width = 0;
if(!left) while(width--) {*ptr++ = pad; ++cc;}
while(length--) {*ptr++ = *sptr++; ++cc; }
if(left) while(width--) {*ptr++ = pad; ++cc;}
}
*ptr = 0;
return(cc);
}